package com.example.yeujian5.yuejian5.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.yeujian5.yuejian5.Listenner.UserOrderInfoListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.bean.UserOrderInfo;
import com.example.yeujian5.yuejian5.model.UserModel;
import com.squareup.picasso.Picasso;

public class SignInfoActivity extends AppCompatActivity {
    private ImageButton back;
    private ImageView pic;
    private TextView name, state, price, time, address, type, number, pay, update_time;
    private Button button;
    private UserOrderInfo info;
    private String id, sign_name, sign_state, sign_price, sign_time, sign_address, sign_type, sign_number, sign_pay, sign_update_time, sign_photo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_info);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        initViews();
        initEvents();
        SignInfo();
    }

    public void initViews() {
        back = (ImageButton) findViewById(R.id.sign_info_back);
        pic = (ImageView) findViewById(R.id.sign_info_pic);
        name = (TextView) findViewById(R.id.sign_info_name);
        state = (TextView) findViewById(R.id.sign_info_state);
        price = (TextView) findViewById(R.id.sign_info_price);
        time = (TextView) findViewById(R.id.sign_info_time);
        address = (TextView) findViewById(R.id.sign_info_address);
        type = (TextView) findViewById(R.id.sign_info_type);
        number = (TextView) findViewById(R.id.sign_info_number);
        pay = (TextView) findViewById(R.id.sign_info_pay);
        update_time = (TextView) findViewById(R.id.sign_info_update_time);
        button = (Button) findViewById(R.id.sign_info_button);
    }

    public void initEvents() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deletbuy();
            }
        });
    }

    private void deletbuy() {
        UserModel model=new UserModel();
        model.NoSign(id, "2", UserdeleteOrderListener);
    }

     UserOrderInfoListener UserdeleteOrderListener = new UserOrderInfoListener() {
        @Override
        public void onResponse(UserOrderInfo listBean) {
            info = listBean;
            Toast.makeText(SignInfoActivity.this,"取消订单成功",Toast.LENGTH_LONG).show();
//            initData();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SignInfoActivity.this,"取消订单失败",Toast.LENGTH_LONG).show();
        }
    };

    private void SignInfo() {
        UserModel model=new UserModel();
        model.getSignByID(id, "2", UserOrderInfoListener);
    }

    UserOrderInfoListener UserOrderInfoListener = new UserOrderInfoListener() {
        @Override
        public void onResponse(UserOrderInfo listBean) {
            info = listBean;
            initData();
        }

        @Override
        public void onFail(String msg) {
        }
    };

    private void initData() {
        sign_name = info.getName();
        sign_state = info.getVstate();
        sign_price = info.getPrice();
        sign_time = info.getStart_date();
        sign_address = info.getTel();
        sign_type = info.getDegree();
        sign_number = info.getAdultnum();
        sign_pay = info.getPay();
        sign_update_time = info.getUpdate_time();
        sign_photo = info.getThumb();

        name.setText(sign_name);
        if(sign_state.equals("0")){
            state.setText("未参加");
        } else {
            state.setText("已参加");
        }
        price.setText("订单单价："+sign_price);
        time.setText("开始时间："+sign_time);
        address.setText("活动电话："+sign_address);
        if(sign_type.equals("1")){
            type.setText("订单票类：成人票");
        } if(sign_type.equals("2")) {
            type.setText("订单票类：学生票");
        } else {
            type.setText("订单票类：儿童票");
        }
        number.setText("购买数量："+sign_number);
        pay.setText("订单总计："+sign_pay);
        update_time.setText("下单时间："+sign_update_time);
        if (sign_photo != ""){
            Picasso.with(SignInfoActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/"+sign_photo)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        }else{
            pic.setImageResource(R.drawable.qya);
        }

    }
}
